package com.von.nhcf.controller;

import com.von.nhcf.api.MysqlRemoteService;
import com.von.nhcf.constant.NhcfConstant;
import com.von.nhcf.entity.vo.AddressVO;
import com.von.nhcf.entity.vo.MemberLoginVO;
import com.von.nhcf.entity.vo.OrderProjectVO;
import com.von.nhcf.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderConsumerController {
    @Autowired
    MysqlRemoteService mysqlRemoteService;

    @RequestMapping("/do/save/address")
    public String doSaveAddress(AddressVO addressVO, HttpSession session) {
        // 保存新增的地址
        mysqlRemoteService.saveAddressRemote(addressVO);
        // 获取returnCount值
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute(NhcfConstant.ATTR_NAME_ORDER_PROJECT_VO);
        Integer returnCount = orderProjectVO.getReturnCount();
        return "redirect:/order/confirm/order/" + returnCount;
    }
    @RequestMapping("/confirm/return/info/{projectId}/{returnId}")
    public String getOrderConfirmInfo(@PathVariable("projectId")Integer projectId,
                                      @PathVariable("returnId") Integer returnId,
                                      HttpSession session) {
        ResultEntity<OrderProjectVO> resultEntity = mysqlRemoteService.getProjectVORemote(projectId, returnId);
        if (ResultEntity.getSUCCESS().equals(resultEntity.getResult())) {
            OrderProjectVO data = resultEntity.getData();
           session.setAttribute(NhcfConstant.ATTR_NAME_ORDER_PROJECT_VO, data);
        }

        return "order_confirm";
    }

    @RequestMapping("/confirm/order/{returnCount}")
    public String doConfirmOrder(@PathVariable("returnCount")Integer returnCount, HttpSession session) {
        // 合并returnCount值
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute(NhcfConstant.ATTR_NAME_ORDER_PROJECT_VO);
        orderProjectVO.setReturnCount(returnCount);
        session.setAttribute(NhcfConstant.ATTR_NAME_ORDER_PROJECT_VO, orderProjectVO);
        // 保存收货地址到session
        MemberLoginVO memberLoginVO = (MemberLoginVO) session.getAttribute(NhcfConstant.ATTR_NAME_MEMBER);
        Integer memberId = memberLoginVO.getId();
        ResultEntity<List<AddressVO>> addressVOListResult = mysqlRemoteService.getAddressVORemote(memberId);
        if (ResultEntity.getSUCCESS().equals(addressVOListResult.getResult())) {
            session.setAttribute(NhcfConstant.ATTR_NAME_ADDRESS_VO_LIST, addressVOListResult.getData());
        }

        return "order_pay_info";
    }

}
